package me.raih.service.impl;

import me.raih.mapper.AppointmentMapper;
import me.raih.pojo.po.Appointment;
import me.raih.pojo.po.User;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.service.AppointmentService;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Raih
 * @Time 2021/1/28  18:54
 */
@Service
@Transactional
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentMapper appointmentMapper;

    @Autowired
    WaresService waresService;

    @Autowired
    UserService userService;


    @Override
    public int changeUserWaresStatus(String username, String waresNo) {
        //更改状态前先查一下有没有
        Appointment appointment = appointmentMapper.findAppointmentMessage(username, waresNo);
        if(appointment != null){
            //如果有  删除
            int t = appointmentMapper.deleteUserWaresStatus(username, waresNo);
            if(t == 1){
                return 0;
            }else {
                //删除出现异常时候
                return -1;
            }
        }else {
            //如果没有就添加呗
            int t = appointmentMapper.addUserWaresStatus(username, waresNo);
            if(t == 1){
                return 1;
            }else {
                //添加出现异常时候
                return -1;
            }
        }
    }

    @Override
    public int findAppointmentStatus(String username, String waresNo) {

        Appointment appointment = appointmentMapper.findAppointmentMessage(username, waresNo);
        if (appointment != null) {
            //如果不为空 就是查到了
            return 1;
        }
        return 0;
    }

    @Override
    public int waresAppointmentCount(String waresNo) {

        return appointmentMapper.findWaresAppointmentCount(waresNo);
    }

    @Override
    public List<Appointment> findAppointmentByUsername(String username) {

        return  appointmentMapper.findAppointmentByUsername(username);
    }

    @Override
    public PageBean<Object> findAppointmentList(int currentPage, int pageSize, String param, int bigPage) {

        if("search".equals(param)){
            currentPage = 1;
        }else if("forward".equals(param)){
            if(currentPage > 1){
                currentPage --;
            }
        }else if("begin".equals(param)){
            if(currentPage != 1){
                currentPage = 1;
            }
        }else if("nextPage".equals(param)){
            if(currentPage < bigPage){
                currentPage ++;
            }
        }else if("end".equals(param)){
            if(currentPage < bigPage){
                currentPage = bigPage;
            }
        }else {
            currentPage = 1;
        }

        List<Appointment> appointments = appointmentMapper.findAppointmentByPage((currentPage-1)*pageSize,pageSize);
        int maxNumber = appointmentMapper.findAppointmentByPageNumber();
        //定义map
        List<Object> list = new ArrayList<>();
        //遍历预约记录
        for (int i = 0; i < appointments.size(); i++) {
            //Map<Map<Integer,User>,Map<Integer, Wares>> map = new HashMap<>();
            //Map<Integer, Wares> waresMap = new HashMap<>();
            //Map<Integer, User> userMap = new HashMap<>();
            //获取商品号
            String no = appointments.get(i).getNo();
            //通过商品号查找商品
            Wares wares = waresService.findWaresByNo(no);
            //存起来
            //waresMap.put(i,wares);
            //获取用户名
            String username = appointments.get(i).getUsername();
            //通过用户名查找用户
            User user = userService.findUserByUsername(username);
            //存起来
            //userMap.put(i,user);
            //将信息存在 map 中
            //map.put(userMap,waresMap);
            //list.add(map);
            list.add(wares);
            list.add(user);
        }
        //手动new出来 页面关联
        //List<<Map<Map<Integer,User>,Map<Integer, Wares>>> list = new ArrayList<>();

        PageBean<Object> pageBean = new PageBean<>();

        //赋值
        pageBean.setList(list);
        pageBean.setTotalPage(maxNumber%pageSize==0?(maxNumber/pageSize):((maxNumber/pageSize)+1));
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setTotalCount(maxNumber);

        return pageBean;
    }
}
